package com.smstransmit.control

import android.content.Context
import android.text.TextUtils
import android.util.Log
import android.widget.Toast
import com.xmrecruit.util.PreferenceUtil
import com.xmrecruit.util.ToastUtil

/**
 * Created by shijie.guo on 2017/9/11.
 */
object SmsControl {
    val smtp: String = "smtp"
    val smtp_port: String = "smtp_port"
    val send_address: String = "send_address"
    val send_password: String = "send_password"
    val receive_address: String = "receive_address"

    fun send(context: Context, content: String = "test") {
        val smtp = PreferenceUtil.getString(smtp, "")
        val smtp_port = PreferenceUtil.getString(smtp_port, "")
        val send_address = PreferenceUtil.getString(send_address, "")
        val send_password = PreferenceUtil.getString(send_password, "")
        val receive_address = PreferenceUtil.getString(receive_address, "")
        if (TextUtils.isEmpty(smtp)
                || TextUtils.isEmpty(smtp_port)
                || TextUtils.isEmpty(send_address)
                || TextUtils.isEmpty(send_password)
                || TextUtils.isEmpty(receive_address)
                ) {
            Toast.makeText(context, "请保存您的配置文件", Toast.LENGTH_SHORT).show()
            return
        }
        //耗时操作要起线程...有几个新手都是这个问题
        Thread(Runnable {
            try {
                val sender = EmailSender()
                //设置服务器地址和端口，网上搜的到
                sender.setProperties(smtp, smtp_port)
                //分别设置发件人，邮件标题和文本内容
                sender.setMessage(send_address, "短信转发", content)
                //设置收件人
                sender.setReceiver(arrayOf(receive_address))
                //添加附件
                //这个附件的路径是我手机里的啊，要发你得换成你手机里正确的路径
                //                          sender.addAttachment("/sdcard/DCIM/Camera/asd.jpg");
                //发送邮件
                sender.sendEmail(smtp, send_address, send_password)
                Log.e("sms", "发送成功")
            } catch (e: Exception) {
                e.printStackTrace()
            }
        }).start()
    }
}